/*
 * Copyright 2010 Alexander Ulizko
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ulizko.template.soy.javaSrc;

import com.ulizko.template.soy.utils.StringUtils;
import japa.parser.ASTHelper;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.PackageDeclaration;
import japa.parser.ast.body.ClassOrInterfaceDeclaration;
import japa.parser.ast.body.JavadocComment;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.ModifierSet;

import java.util.ArrayList;
import java.util.List;

/**
 * Blank for future java class.
 * It stores class name, package name, methods of the future class.
 * And generates java class source on top of all of it.
 * <p/>
 * All the magic contains {@link #generateFileContent} method.
 *
 * @author Alexander <alexander@ulizko.com> Ulizko
 */
public class JavaClassBlank {
    /**
     * Package name, like "com.sitename.template"
     */
    private String packageName;
    /**
     * Class name, like SomeTemplate, Page, or so.
     */
    private String className;
    /**
     * Collection of methods, used by {@link #generateFileContent} to generate class's source.
     */
    private List<MethodDescription> methods = new ArrayList<MethodDescription>();

    /**
     * Returns package name.
     * @return Package name, like "com.site.template".
     */
    public String getPackageName() {
        return packageName;
    }

    /**
     * Set package name.
     * @param packageName Package name, like "com.site.template".
     */
    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    /**
     * Returns java class name.
     * @return Class name, like SomeTemplate or Page or so.
     */
    public String getClassName() {
        return className;
    }

    /**
     * Set java class name.
     * @param className Class name, like SomeTemplate or Page or so.
     */
    public void setClassName(String className) {
        this.className = className;
    }

    /**
     * Add method to the methods list.
     * Used internally by {@link BlobJavaSrcAnalyzer}.
     *
     * @param method Method which belongs to current class.
     */
    public void addMethod(MethodDescription method) {
        methods.add(method);
    }

    /**
     * Returns method list.
     * @return Methods list.
     */
    public List<MethodDescription> getMethods() {
        return methods;
    }

    /**
     * Generates java class source based.
     * <p/>
     * Java class name taken as a second-to-last slice of generated by {@link SoyFileSetBuilder#compileToJavaSrc()} methods.
     * For details, see {@link com.ulizko.template.soy.javaSrc.MethodDescription#getClassName}.
     * <p/>
     * Java package name taken from generated by {@link SoyFileSetBuilder#compileToJavaSrc()} methods. Usually equals to soy's namespace.
     * For details, see {@link com.ulizko.template.soy.javaSrc.MethodDescription#getClassName}.
     * <p/>
     * Class body generates from methods list. Each method name is generated, as described at {@link com.ulizko.template.soy.javaSrc.MethodDescription#getClassName},
     * each method has a valid javadoc with note about from which soy file this method has been generated.
     * <p/>
     * The whole magic is done by Julio's Vilmar Gesser <a href="http://code.google.com/p/javaparser/">JavaParser</a>. 
     * @return Human-readable java class source.
     */
    public String generateFileContent() {
        CompilationUnit compilationUnit = new CompilationUnit();

        // set the package
        compilationUnit.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(packageName)));

        // create the type declaration
        ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, className);
        ASTHelper.addTypeDeclaration(compilationUnit, type);

        // add methods
        for (MethodDescription methodDescripton : methods) {
            MethodDeclaration method = methodDescripton.getMethod();

            // generate javadoc
            JavadocComment javaDoc = new JavadocComment();
            javaDoc.setContent(StringUtils.generateJavaDocContent(methodDescripton.getSoyFileName()));

            method.setJavaDoc(javaDoc);
            ASTHelper.addMember(type, method);

        }

        return compilationUnit.toString();
    }
}
